﻿package com.wly.ui
{
	import com.wly.adapter.MapAreaAdapter;
	import com.wly.adapter.MapEditerMainAdapter;
	import com.wly.ui.common.IItemGrid;
	import com.wly.ui.common.SpriteObject;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	public class MapArea extends SpriteObject implements IItemGrid
	{
		private var _hightNum:int;
		private var _widthNum:int;
		private var _items:Array;
		private var _dragStat:int = 0;
		
		private const MAPWIDTH:int = 800;
		private const MAPHEIGHT:int  = 480;
		
		public function MapArea()
		{
			super();
			init();
		}
		
		private function init():void
		{
			var i:int;
			var j:int;
			var item:ItemCell;
			
			_items = [];
			
			//边框有一个像素
			_widthNum = (width-2)/ItemCell.WIDTH+1;
			_hightNum = (height-2)/ItemCell.HIGHT+1;

			for(i=0; i<_hightNum; i++)
			{
				for(j=0; j< _widthNum; j++)
				{
					item = new ItemCell;
					item.parentUI = this;
					item.x = j*ItemCell.WIDTH;
					item.y = i*ItemCell.HIGHT;
					item.index = i*_widthNum+j;
					addChild(item);
					_items.push(item);
				}
			}
			addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			addEventListener(MouseEvent.MOUSE_UP, onUp);
		}
		
		private function onDown(e:MouseEvent):void
		{
			_dragStat =1;
		}
		
		private function onUp(e:MouseEvent):void
		{
			_dragStat = 0;
		}
		
		protected function get mapAreaAdapter():MapAreaAdapter
		{
			return adapter as MapAreaAdapter;
		}
		
		public function onSelect(index:int):void
		{
		//	doCell(index);
			var item:ItemCell = _items[index] as ItemCell;
			if(mapAreaAdapter && item)
			{
				if(mapAreaAdapter.workStatus == MapAreaAdapter.EDIT_MAP)
				{
					item.updateBmd(MapEditerMainAdapter.CURBMD, MapEditerMainAdapter.MAPINDEX);
				}
				else if(mapAreaAdapter.workStatus == MapAreaAdapter.EDIT_ROAD)
				{
					mapAreaAdapter.ChgCellStat(index);
				}
			}
		}
		
		public function setAllCell():void
		{
			var i:int;
			var item:ItemCell;;
			
			if(mapAreaAdapter.workStatus == MapAreaAdapter.EDIT_MAP)
			{
				for(i=0; i<_items.length; i++)
				{
					item = _items[i] as ItemCell;
					item.updateBmd(MapEditerMainAdapter.CURBMD, MapEditerMainAdapter.MAPINDEX);	
				}
			}
		}
		
		public function clearAllCell():void
		{
			var i:int;
			var item:ItemCell;
			var statArr:Array;
			
			if(mapAreaAdapter.workStatus == MapAreaAdapter.EDIT_MAP)
			{
				for(i=0; i<_items.length; i++)
				{
					item = _items[i] as ItemCell;
					item.updateBmd(null, MapEditerMainAdapter.MAPINDEX);	
				}
			}
			else
			{
				if(mapAreaAdapter)
				{
					mapAreaAdapter.clearAllCellStatus();
				}
			}
		}
		
		public function setTop(displayObject:DisplayObject):void
		{
			if(displayObject && displayObject.parent==this)
			{
				setChildIndex(displayObject, numChildren-1);
			}
		}
		
		public function updateMap(bmd:BitmapData):void
		{
			var i:int, j:int;
			var item:ItemCell;
			var cellBmd:BitmapData;
			var rect:Rectangle = new Rectangle(0, 0, ItemCell.WIDTH, ItemCell.HIGHT);
			
			if(bmd)
			{
				for(i=0; i<_hightNum; i++)
				{
					for(j=0; j< _widthNum; j++)
					{
						cellBmd = new BitmapData(ItemCell.WIDTH, ItemCell.HIGHT);
						item = _items[i*_widthNum+j] as ItemCell;
						rect.x = j*ItemCell.WIDTH;
						rect.y = i*ItemCell.HIGHT;
						cellBmd.copyPixels(bmd, rect,new Point(0,0))
						if(cellBmd && item)
						{
							item.updateBmd(cellBmd);
						}
					}
				}
			}
		}
		
		public function updateWorkStat(stat:int):void
		{
			switch(stat)
			{
				case MapAreaAdapter.EDIT_MAP:
					_dragStat = 0;
					setMapEdit();
					Status.getInstance().text = "地图编辑";
					break;
				case MapAreaAdapter.EDIT_ROAD:
					_dragStat = 0;
					setRoadEdit();
					Status.getInstance().text = "路径编辑";
					break;
			}		
		}
		
		public function setRoadCellStat(index:int):void
		{
			var stat:int = mapAreaAdapter.getCellStatus()[index] as int;
			setCellStat(index, stat);
		}
		
		private function setMapEdit():void
		{
			var i:int;
			for(i=0; i<_items.length; i++)
			{
				setCellStat(i, MapAreaAdapter.STAT_CELL_VALID);
			}
		}
		
		private function setRoadEdit():void
		{
			var i:int;
			var stat:int;
			var arr:Array;
			
			arr = mapAreaAdapter.getCellStatus();
			
			for(i=0; i<_items.length; i++)
			{
				stat = arr[i] as int;
				setCellStat(i, stat);	
			}
		}
		
		private function setCellStat(index:int, stat:int):void
		{
			var itemCell:ItemCell;
			itemCell = _items[index] as ItemCell;
			
			switch(stat)
			{
				case MapAreaAdapter.STAT_CELL_VALID:
					itemCell.alpha = 1;
					break;
				case MapAreaAdapter.STAT_CELL_UNVALID:
					itemCell.alpha = 0.5;
					break;
			}
		}
		
		public function onItemMouseOver(index:int):void
		{
			doCell(index);
		}
		
		private function doCell(index:int):void
		{
			var item:ItemCell = _items[index] as ItemCell;
			if(_dragStat)
			{
				if(mapAreaAdapter && item)
				{
					if(mapAreaAdapter.workStatus == MapAreaAdapter.EDIT_MAP)
					{
						item.updateBmd(MapEditerMainAdapter.CURBMD, MapEditerMainAdapter.MAPINDEX);
					}
					else if(mapAreaAdapter.workStatus == MapAreaAdapter.EDIT_ROAD)
					{
						mapAreaAdapter.ChgCellStat(index);
					}
				}
			}
		}
	}
}